home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_005 / serial / serial.c < prev   
C/C++ Source or Header  |  1992-05-06  |  9KB  |  339 lines

  1. /*
  2.  *
  3.  *    DISCLAIMER:
  4.  *
  5.  *    This program is provided as a service to the programmer
  6.  *    community to demonstrate one or more features of the Amiga
  7.  *    personal computer.  These code samples may be freely used
  8.  *    for commercial or noncommercial purposes.
  9.  * 
  10.  *     Commodore Electronics, Ltd ("Commodore") makes no
  11.  *    warranties, either expressed or implied, with respect
  12.  *    to the program described herein, its quality, performance,
  13.  *    merchantability, or fitness for any particular purpose.
  14.  *    This program is provided "as is" and the entire risk
  15.  *    as to its quality and performance is with the user.
  16.  *    Should the program prove defective following its
  17.  *    purchase, the user (and not the creator of the program,
  18.  *    Commodore, their distributors or their retailers)
  19.  *    assumes the entire cost of all necessary damages.  In 
  20.  *    no event will Commodore be liable for direct, indirect,
  21.  *    incidental or consequential damages resulting from any
  22.  *    defect in the program even if it has been advised of the 
  23.  *    possibility of such damages.  Some laws do not allow
  24.  *    the exclusion or limitation of implied warranties or
  25.  *    liabilities for incidental or consequential damages,
  26.  *    so the above limitation or exclusion may not apply.
  27.  *
  28.  */
  29.  
  30. #include        "exec/types.h"
  31. #include        "exec/nodes.h"
  32. #include        "exec/lists.h"
  33. #include        "exec/ports.h"
  34. #include        "exec/libraries.h"
  35. #include        "exec/devices.h"
  36. #include        "exec/io.h"
  37. #include        "devices/serial.h"
  38.  
  39. struct IOExtSer *IORser;
  40. struct MsgPort *port;
  41. char   buffer[200];
  42. extern struct MsgPort *CreatePort();
  43. extern struct IORequest *CreateExtIO();
  44.  
  45. /* note:  to run this program, you must have an external terminal, set
  46.  * at 9600 baud, attached to the Amiga serial port.  Additionally the
  47.  * serial.device file must be located in the directory currently 
  48.  * assigned to DEVS:  (to check this, in AmigaDOS, type:  ASSIGN 
  49.  * then check the directory (usually the boot CLI disk volume, devs directory.
  50.  */
  51.  
  52. main()
  53. {
  54.     int     error;
  55.     int     actual;
  56.     unsigned long rbl;
  57.     unsigned long brk;
  58.     unsigned long baud;
  59.     unsigned char rwl;
  60.     unsigned char wwl;
  61.     unsigned char sf;
  62.     unsigned long t0;
  63.     unsigned long t1;
  64.  
  65.     /* SET UP the message port in the I/O request */
  66.     port = CreatePort (SERIALNAME,0);
  67.     if (port == NULL) {
  68.         printf("\nProblems during CreatePort");
  69.         exit(100);
  70.     }
  71.  
  72.     /* Create the request block for passing info to and from the serial device. */
  73.     IORser = (struct IOExtSer *)CreateExtIO(port,sizeof(struct IOExtSer));
  74.     if (IORser == NULL) 
  75.     { 
  76.         printf("\nProblems during CreateExtIO"); 
  77.         goto cleanup1; 
  78.     }
  79.  
  80. open:
  81.     /* OPEN the serial.device */
  82.     if ((error = OpenDevice (SERIALNAME, 0, IORser, 0)) != 0) {
  83.         printf ("Serial device did not open, error = %ld",error);
  84.         goto cleanup1;
  85.     }
  86.  
  87.     /*    SET PARAMS for the serial.device */
  88.     rbl = 4096;
  89.     rwl = 0x08;
  90.     wwl = 0x08;
  91.     brk = 750000;
  92.     baud= 9600;
  93.     sf  = 0x00;
  94.     t0  = 0x51040303;
  95.     t1  = 0x03030303;
  96.  
  97.     if ((error = SetParams (IORser,rbl,rwl,wwl,brk,baud,sf,t0,t1)) != 0) {
  98.         printf ("Set parameters command returned an error: %ld",error);
  99.         goto cleanup2;
  100.     } 
  101.  
  102.     printf("\nSerial Device opened and accepted parameters");
  103.     WriteSer (IORser,"\n\015Device opened ok\n\015", -1);
  104.  
  105.     printf("\nTesting character exact-count output thru SendWaitWrite");
  106.     SendWaitWrite (IORser,
  107.         "User counts characters in string to send\n\015", 42);
  108.  
  109.     printf("\nTest string length of -1 (make system find end of string)");
  110.     SendWaitWrite (IORser,
  111.         "or if null terminated string, say '-1'\n\015", -1);
  112.  
  113.     printf("\nType 16 characters to send to amiga...");
  114.     printf("\nIf no external terminal is attached, waits forever!!");
  115.     WriteSer (IORser,
  116.         "\n\015Type 16 characters to send to amiga\n\015", -1);
  117.     actual = ReadSer (IORser,buffer,16);
  118.     WriteSer (IORser,
  119.         "\n\015You typed these printable characters:\n\015", -1); 
  120.     WriteSer (IORser,buffer, actual); 
  121.     WriteSer (IORser,"\n\015End of test\n\015", -1); 
  122.     WriteSer (IORser,"54321.....exit\n\015", 16); 
  123.     printf("\nTest completed!\n");
  124.  
  125.     /* CLOSE the serial.device */
  126. cleanup2:
  127.     CloseDevice (IORser);
  128. cleanup1:
  129.     DeletePort (port);
  130.     exit (0);
  131. }
  132.  
  133. /* SERIAL I/O functions */
  134.  
  135. SetParams(io,rbuf_len,rlen,wlen,brk,baud,sf,ta0,ta1)
  136.  
  137. struct IOExtSer *io;
  138. unsigned long rbuf_len;
  139. unsigned char rlen;
  140. unsigned char wlen;
  141. unsigned long brk;
  142. unsigned long baud;
  143. unsigned char sf;
  144. unsigned long ta0;
  145. unsigned long ta1;
  146.  
  147. {
  148.     int error;
  149.  
  150.     io->io_ReadLen       = rlen;
  151.     io->io_BrkTime       = brk;
  152.     io->io_Baud          = baud;
  153.     io->io_WriteLen      = wlen;
  154.     io->io_StopBits      = 0x01;
  155.     io->io_RBufLen       = rbuf_len;
  156.     io->io_SerFlags      = sf;
  157.     io->IOSer.io_Command = SDCMD_SETPARAMS;
  158.     io->io_TermArray.TermArray0 = ta0;
  159.     io->io_TermArray.TermArray1 = ta1;
  160.  
  161.     if ((error = DoIO (io)) != 0) {
  162.         printf ("serial.device setparams error %ld \n", error);
  163.     }
  164.     return (error);
  165. }
  166.  
  167. ReadSer(io,data,length)
  168. struct IOExtSer *io;
  169. char *data;
  170. int length;
  171. {
  172.     int error;
  173.  
  174.     io->IOSer.io_Data = (APTR)data;
  175.     io->IOSer.io_Length = (ULONG)length;
  176.     io->IOSer.io_Command = CMD_READ;
  177.  
  178.     if ((error = DoIO (io)) != 0) {
  179.         printf ("serial.device read error %ld \n", error);
  180.     }
  181.     return ((int)io->IOSer.io_Actual);
  182. }
  183.  
  184.  
  185. WriteSer(io,data,length)
  186. struct IOExtSer *io;
  187. char *data;
  188. int length;
  189. {
  190.     int     error;
  191.  
  192.     io->IOSer.io_Data = (APTR)data;
  193.     io->IOSer.io_Length = length;
  194.     io->IOSer.io_Command = CMD_WRITE;
  195.  
  196.     if ((error = DoIO (io)) != 0) {
  197.         printf ("serial.device write error %ld \n", error);
  198.     }
  199.     return (error);
  200. }
  201.  
  202. SendWaitWrite(io,data,length)
  203. struct IOExtSer *io;
  204. char *data;
  205. int length;
  206. {
  207.     int     error;
  208.  
  209.     io->IOSer.io_Data = (APTR)data;
  210.     io->IOSer.io_Length = length;
  211.     io->IOSer.io_Command = CMD_WRITE;
  212.  
  213.     SendIO (io);
  214.  
  215.     if ((error = WaitIO (io)) != 0) {
  216.         printf ("serial.device waitio error %ld \n", error);
  217.     }
  218.     return ((int)io->IOSer.io_Actual);
  219. }
  220.  
  221.  
  222. /***********************************************************************
  223. *
  224. *    Exec Support Function -- Extended IO Request
  225. *
  226. ***********************************************************************/
  227.  
  228. #include "exec/types.h"
  229. #include "exec/nodes.h"
  230. #include "exec/lists.h"
  231. #include "exec/memory.h"
  232. #include "exec/interrupts.h"
  233. #include "exec/ports.h"
  234. #include "exec/libraries.h"
  235. #include "exec/io.h"
  236. #include "exec/tasks.h"
  237. #include "exec/execbase.h"
  238.  
  239. extern APTR AllocMem();
  240.  
  241. /****** exec_support/CreateExtIO **************************************
  242. *
  243. *   NAME    
  244. *    CreateExtIO() -- create an Extended IO request
  245. *
  246. *   SYNOPSIS
  247. *    ioReq = CreateExtIO(ioReplyPort,size);   
  248. *
  249. *   FUNCTION
  250. *    Allocates memory for and initializes a new IO request block
  251. *    of a user-specified number of bytes.
  252. *
  253. *   INPUTS
  254. *    ioReplyPort - a pointer to an already initialized
  255. *        message port to be used for this IO request's reply port.
  256. *
  257. *   RESULT
  258. *    Returns a pointer to the new block.  Pointer is of the type
  259. *    struct IORequest.
  260. *
  261. *    0 indicates inability to allocate enough memory for the request block
  262. *    or not enough signals available.
  263. *
  264. *   EXAMPLE
  265. *    struct IORequest *myBlock;
  266. *    if( (myBlock = CreateExtIO(myPort,sizeof(struct IOExtTD)) == NULL)
  267. *        exit(NO_MEM_OR_SIGNALS);
  268. *
  269. *    example used to allocate space for IOExtTD (trackdisk driver
  270. *    IO Request block for extended IO operations).
  271. *
  272. *   SEE ALSO
  273. *    DeleteExtIO
  274. *
  275. ***********************************************************************/
  276.  
  277. struct IORequest *CreateExtIO(ioReplyPort,size)
  278.     struct MsgPort *ioReplyPort;
  279.     LONG size;
  280. {
  281.     struct IORequest *ioReq;
  282.  
  283.     if (ioReplyPort == 0)
  284.     return ((struct IORequest   *) 0);
  285.  
  286.     ioReq = (struct IORequest *)AllocMem (size, MEMF_CLEAR | MEMF_PUBLIC);
  287.  
  288.     if (ioReq == 0)
  289.     return ((struct IORequest   *) 0);
  290.  
  291.     ioReq -> io_Message.mn_Node.ln_Type = NT_MESSAGE;
  292.     ioReq -> io_Message.mn_Node.ln_Pri = 0;
  293.  
  294.     ioReq -> io_Message.mn_ReplyPort = ioReplyPort;
  295.  
  296.     return (ioReq);
  297. }
  298.  
  299. /****** exec_support/DeleteExtIO **************************************
  300. *
  301. *   NAME
  302. *    DeleteExtIO() - return memory allocated for extended IO request
  303. *
  304. *   SYNOPSIS
  305. *    DeleteExtIO(ioReq,size);
  306. *
  307. *   FUNCTION
  308. *    See summary line at NAME.  Also frees the signal bit which
  309. *    had been allocated by the call to CreateExtIO.
  310. *
  311. *   INPUTS
  312. *    A pointer to the IORequest block whose resources are to be freed.
  313. *
  314. *   RESULT
  315. *    Frees the memory.  Returns (no error conditions shown)
  316. *
  317. *   EXAMPLE
  318. *    struct IORequest *myBlock;
  319. *    DeleteExtIO(myBlock,(sizeof(struct IOExtTD)));
  320. *        
  321. *    example shows that CreateExtIO had been used to create a trackdisk
  322. *    (extended) IO Request block.
  323. *
  324. *   SEE ALSO
  325. *    CreateExtIO
  326. *
  327. **************************************************************************/
  328.  
  329. DeleteExtIO(ioExt,size)
  330.     struct IORequest *ioExt;
  331.     LONG size;
  332. {
  333.     ioExt -> io_Message.mn_Node.ln_Type = 0xff;
  334.     ioExt -> io_Device = (struct Device *) -1;
  335.     ioExt -> io_Unit = (struct Unit *) -1;
  336.  
  337.     FreeMem (ioExt, size);
  338. }
  339.